using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Nirvana.SkillEditor;
using UnityEditor;
using UnityEngine;
using UnityEditor.IMGUI.Controls;
using UnityEngine.Playables;

namespace Nirvana.SkillEditor
{
    public class SkillFileTreeViewItem : TreeViewItem
    {
        public string fullPath;

        public SkillFileTreeViewItem(int id, int depth, string displayName, string fullPath) : base(id, depth, displayName)
        {
            this.fullPath = fullPath;
        }
    }

    public class SkillFileTreeView : TreeView
    {
        public SkillFileTreeView(TreeViewState state) : base(state)
        {
            showAlternatingRowBackgrounds = true;
            showBorder = true;
            Reload();
        }

        protected override TreeViewItem BuildRoot()
        {
            var root = new TreeViewItem(0, -1, "root");
            TraceBuildTree(root, Application.dataPath + "/NirvanaToolkit/SkillEditor/SkillData");
            return root;
        }

        private void TraceBuildTree(TreeViewItem item, string path)
        {
            DirectoryInfo rootFolder = new DirectoryInfo(path);
            DirectoryInfo[] dirInfo = rootFolder.GetDirectories();
            var files = rootFolder.GetFiles("*.playable", SearchOption.TopDirectoryOnly);
            if (dirInfo.Length > 0)
            {
                foreach (var nextDepthFolder in dirInfo)
                {
                    string assetPath = nextDepthFolder.FullName.ToAssetPath();
                    var folderItem = new SkillFileTreeViewItem(assetPath.GetHashCode(), item.depth + 1, nextDepthFolder.Name, assetPath);
                    item.AddChild(folderItem);
                    TraceBuildTree(folderItem, path + "/" + nextDepthFolder.Name);
                }
            }

            foreach (var file in files)
            {
                string assetPath = file.FullName.ToAssetPath();
                item.AddChild(new SkillFileTreeViewItem(assetPath.GetHashCode(), item.depth + 1, file.Name.Replace(".playable", ""), assetPath));
            }
        }

        protected override void SelectionChanged(IList<int> selectedIds)
        {
            base.SelectionChanged(selectedIds);
            var item = FindItem(selectedIds[0], rootItem) as SkillFileTreeViewItem;
            if (item != null && item.fullPath.Length > 0)
            {
                var asset = AssetDatabase.LoadAssetAtPath<PlayableAsset>(item.fullPath);
                SkillEditorWindow.SetSelectPlayableAsset(asset);
                Selection.activeObject = asset;
            }
            
            EditorApplication.ExecuteMenuItem("Window/Sequencing/Timeline");
        }

        public void DeleteSelects()
        {
            foreach (var id in GetSelection())
            {
                var item = FindItem(id, rootItem) as SkillFileTreeViewItem;
                if (item != null && item.fullPath.Length > 0)
                {
                    AssetDatabase.DeleteAsset(item.fullPath);
                    Reload();
                }
            }
        }
    }
}